package com.example.testapp.activity.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.testapp.R;
import com.example.testapp.activity.model.NewsItem;
import com.example.testapp.activity.util.DateUtils;

import java.util.List;

/**
 * Created by l on 2016/9/18.
 */
public class HotsNewAdapter extends BaseAdapter {

    private Context  mContext;
    private List<NewsItem> mList;

    public HotsNewAdapter(Context context, List<NewsItem> newsItems) {
           super();
        this.mContext = context;
        this.mList = newsItems;
    }

    @Override
    public int getCount() {
        if (null != mList){
            return mList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null){
            convertView = View.inflate(mContext, R.layout.view_item_home_news, null);
            holder = new ViewHolder();
            holder.title = (TextView)convertView.findViewById(R.id.hots_title);
            holder.summary = (TextView)convertView.findViewById(R.id.hots_summary);
            holder.published = (TextView)convertView.findViewById(R.id.hots_publish);
            holder.viewsTv = (TextView)convertView.findViewById(R.id.views_tx);
            holder.diggsTv = (TextView)convertView.findViewById(R.id.diggs_tx);
            holder.sourceName = (TextView)convertView.findViewById(R.id.hots_sourceName);
            holder.commentsTv = (TextView)convertView.findViewById(R.id.comment_tx);
            holder.views = (LinearLayout)convertView.findViewById(R.id.hots_views);
            holder.diggs = (LinearLayout)convertView.findViewById(R.id.hots_diggs);
            holder.comment = (LinearLayout)convertView.findViewById(R.id.hots_comment);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        NewsItem item = mList.get(position);
        holder.title.setText(item.getTitle());
        holder.sourceName.setText("来自:" + item.getSourceName());
        holder.summary.setText(item.getSummary());

        if(!TextUtils.isEmpty(item.getPublished())){
            holder.published.setText(DateUtils.convertGMTToLoacale(item.getPublished()));
        }

        if(!TextUtils.isEmpty(item.getViews())){
           holder.viewsTv.setText(item.getViews());
        }

        if(!TextUtils.isEmpty(item.getComments())){
            holder.commentsTv.setText(item.getComments());
        }

        if(!TextUtils.isEmpty(item.getDiggs())){
            holder.diggsTv.setText(item.getDiggs());
        }

        return convertView;
    }

    public class ViewHolder{
        TextView title;
        TextView summary;
        TextView published;
        TextView viewsTv;
        TextView diggsTv;
        TextView sourceName;
        TextView commentsTv;
        LinearLayout views;
        LinearLayout diggs;
        LinearLayout comment;
    }
}
